package com.yige.service.mongoService

import com.mongodb.BasicDBObject
import com.mongodb.DBCollection
import com.mongodb.DBObject
import com.yige.common.ArgsUtil
import com.yige.common.SystemConstant
import com.yige.common.ToDBObjectUtil
import com.yige.service.RecordService
import org.springframework.stereotype.Service

/**
 * Created by sunhao on 2015/10/6 0006.
 */

@Service
class MongoRecordService extends BasicMongoServiceImp implements RecordService{


    @Override
    DBCollection getCollection() {
        return mongoTemplate.getCollection(SystemConstant.MONGODB_COL_RECORD)
    }

    @Override
    boolean addRecord(String studentId) {
        ArgsUtil.checkArgs(studentId)
        Map<String,Object> recordInfo = [ _id : UUID.randomUUID().toString(),
                                          student : studentId,
                                          piano : 0,
                                          guitar : 0,
                                          zheng : 0,
                                          ukulele : 0,
                                          sing : 0,
                                          field_nebula : 0,
                                          latin_dance : 0,
                                          sketch : 0,
                                          cartoon : 0,
                                          draw : 0]
        DBObject dbObject = ToDBObjectUtil.toDBObject(recordInfo)
        insertOne(dbObject)
        return true
    }

    @Override
    boolean updateRecord(String studentId, String lessonId) {
        int n = -1
        update(studentId,lessonId,n)
        return true
    }

    @Override
    boolean updateRecordPay(String studentId, String lessonId, int n) {
        update(studentId,lessonId,n)
        return true
    }

    def update(String studentId, String lessonId, int n){
        String[] args = {studentId lessonId}
        ArgsUtil.checkArgs1(args)
        def query = new BasicDBObject()
        query.append("student",studentId)
        DBObject record_db = findOneByQuery(query)
        DBObject record = checkLesson(lessonId,record_db,n)
        updateOne(record_db._id,record)
    }

    def checkLesson(String lessonId, DBObject record, int n){
        if(lessonId.equals(SystemConstant.LESSON_PIANO)){
            record.put("piano",record.piano + n)
        }
        if(lessonId.equals(SystemConstant.LESSON_GUITAR)){
            record.put("guitar",record.guitar + n)
        }
        if(lessonId.equals(SystemConstant.LESSON_ZHENG)){
            record.put("zheng",record.zheng + n)
        }
        if(lessonId.equals(SystemConstant.LESSON_UKULELE)){
            record.put("ukulele",record.ukulele + n)
        }
        if(lessonId.equals(SystemConstant.LESSON_SING)){
            record.put("sing",record.sing + n)
        }
        if(lessonId.equals(SystemConstant.LESSON_FIELD_NEBULA)){
            record.put("field_nebula",record.field_nebula + n)
        }
        if(lessonId.equals(SystemConstant.LESSON_LATIN_DANCE)){
            record.put("latin_dance",record.latin_dance + n)
        }
        if(lessonId.equals(SystemConstant.LESSON_SKETCH)){
            record.put('sketch',record.sketch + n)
        }
        if(lessonId.equals(SystemConstant.LESSON_CARTOON)){
            record.put("cartoon",record.cartoon + n)
        }
        if(lessonId.equals(SystemConstant.LESSON_DRAW)){
            record.put("draw",record.draw + n)
        }
        return record
    }
}
